package customerEnter.Frame.Thread.impl;

import static com.google.common.collect.Maps.newHashMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import customerEnter.Frame.Thread.StackTraceElements;
import customerEnter.Frame.Thread.interfaces.FrameStackTraceElement;

public class FrameStackTraceElementImpl implements FrameStackTraceElement {

	public List<StackTraceElements> getStackTraceElements() {
		Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
		List<StackTraceElements> lElements = new ArrayList<StackTraceElements>();
		for (Entry<Thread, StackTraceElement[]> entry : map.entrySet()) {
			String keyName = entry.getKey().getName();
			if (keyName.contains("http-apr-") && keyName.contains("-exec-")) {

				for (StackTraceElement element : entry.getValue()) {
					String fileName = element.getFileName();
					int number = element.getLineNumber();
					String classNameString = element.getClassName();
					String methodName = element.getMethodName();
					if (classNameString.equals("sun.misc.Unsafe")) {
						break;
					}
					if (number < 1 || isStringValues(classNameString)) {
						continue;
					}
					StackTraceElements frameStackTraceElement = new StackTraceElements();
					frameStackTraceElement.setClassName(classNameString);
					frameStackTraceElement.setFileName(fileName);
					frameStackTraceElement.setMethodName(methodName);
					lElements.add(frameStackTraceElement);
				}
			}
		}
		return lElements;
	}

	static Map<String, String>	map	= newHashMap();

	public boolean isStringValues(String str) {
		if (map.isEmpty()) {
			initMap();
		}
		boolean bool = false;
		for (Entry<String, String> entry : map.entrySet()) {
			if (str.contains(entry.getValue())) {
				bool = true;
				break;
			}
		}
		return bool;
	}

	public void initMap() {
		map.put("1", "sun.misc.Unsafe");
		map.put("2", "java.lang");
		map.put("3", "java.util.concurrent");
		map.put("4", "javax.servlet");
		map.put("5", "com.alibaba.druid");
		map.put("6", "org.apache");
		map.put("7", "org.springframework");
		map.put("9", "sun.reflect");
		map.put("8", "customerEnter.Frame");
	}
}
